﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using Framework.Graphics;
using Framework.Graphics.Light.Bump;
using Framework.Graphics.Light.Shadow;

using Framework.GameObjectManagment;
using Framework.Map;

namespace Framework.Graphics.Light
{
    public class LightManager
    {
        ///<summary>
        /// 1.Step  Draw Shadows in an RenderTarget
        /// 2.Step  Draw TheMap with NormalMap in an RenderTarget
        /// 3.Step  Draw TheMap and over this the ShadowMap
        ///</summary>   

        #region Fields

        private RenderTarget2D _bumpColor;
        private RenderTarget2D _bumpNormal;
        private RenderTarget2D _shadowObjects;

        private GraphicsDevice _graphics;

        private LightControl _shadowManager;
        private BumpManager _bumpManager;

        #endregion

        public void Init(GraphicsDevice GD, Game game)
        {
            _graphics = GD;

            PresentationParameters pp = GD.PresentationParameters;
            int width = pp.BackBufferWidth;
            int height = pp.BackBufferHeight;

            _bumpColor = new RenderTarget2D(GD, width, height);
            _bumpNormal = new RenderTarget2D(GD, width, height);
            _shadowObjects = new RenderTarget2D(GD, width, height);

            _bumpManager = new BumpManager();
            _bumpManager.Init(game.Content, GD);

            _shadowManager = new LightControl();
            _shadowManager.Init(GD, game);
            
        }

        public LightControl ShadowManager
        {
            get { return _shadowManager; }
            set { _shadowManager = value; }
        }

        public BumpManager BumpManager
        {
            get { return _bumpManager; }
            set { _bumpManager = value; }
        }

        public RenderTarget2D BumpColor
        {
            get { return _bumpColor; }
            set { _bumpColor = value; }
        }

        public RenderTarget2D ShadowObjects
        {
            get { return _shadowObjects; }
            set { _shadowObjects = value; }
        }

        public RenderTarget2D BumpNormal
        {
            get { return _bumpNormal; }
            set { _bumpNormal = value; }
        }

        public void DrawBumpColor()
        {
            Camera.TryDraw(_bumpColor, Vector2.Zero, Color.White);
        }

        public void DrawBumpNormal()
        {
            Camera.TryDraw(_bumpNormal, Vector2.Zero, Color.White);
        }

        public void DrawShadowObjects()
        {
            Camera.TryDraw(_shadowObjects, Vector2.Zero, Color.White);
        }

        public void AddBumpColor(Texture2D tex, Vector2 pos)
        {
            _graphics.SetRenderTarget(_bumpColor);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, Color.White);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void AddBumpColor(Texture2D tex, Vector2 pos, float scale, Color color)
        {
            _graphics.SetRenderTarget(_bumpColor);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void AddBumpNormal(Texture2D tex, Vector2 pos)
        {
            _graphics.SetRenderTarget(_bumpNormal);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, Color.White);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void AddBumpNormal(Texture2D tex, Vector2 pos, float scale, Color color)
        {
            _graphics.SetRenderTarget(_bumpNormal);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void AddShadowObjects(Texture2D tex, Vector2 pos)
        {
            _graphics.SetRenderTarget(_shadowObjects);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, Color.White);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void AddShadowObjects(Texture2D tex, Vector2 pos, float scale, Color color)
        {
            _graphics.SetRenderTarget(_shadowObjects);
            Camera.SpriteBatch.Begin();
            Camera.TryDraw(tex, pos, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);
        }

        public void Reset()
        {
            _graphics.SetRenderTarget(_bumpColor);
            _graphics.Clear(Color.Black);
            _graphics.SetRenderTarget(_bumpNormal);
            _graphics.Clear(Color.Black);
            _graphics.SetRenderTarget(_shadowObjects);
            _graphics.Clear(Color.Transparent);
            _graphics.SetRenderTarget(null);
        }

        public void Draw()
        {
            //_bumpManager.Draw(_bumpColor, _bumpNormal,this);
            _shadowManager.Draw(_shadowObjects, this);
        }

        public void Draw(GameObjectManager gom, TileMap map)
        {
            //_bumpManager.Draw(_bumpColor, _bumpNormal,this);
            _shadowManager.Draw(_shadowObjects, this,map,gom);
        }

        public void OptionalUpdate()
        {
            _bumpManager.Update();
        }
    }
}
